<template>
  <div class="root flex">
    <div class="bg"><img src="@/assets/img/background.webp" /></div>
    <div class="c2 card">
      <div class="c3">{{ $t("sys.t9") }}</div>
      <div class="c4">vue3.2 + ts + vite + element-plus</div>
      <div class="c5">
        <form @submit="subLogin">
          <div class="c6">
            <el-input
              :placeholder="$t('sys.t11')"
              :prefix-icon="User"
              :clearable="true"
              size="large"
              v-model.trim="formData.user"
            >
            </el-input>
          </div>
          <div class="c6">
            <el-input
              :placeholder="$t('sys.t12')"
              type="password"
              :show-password="true"
              size="large"
              :clearable="true"
              :prefix-icon="Key"
              v-model.trim="formData.pass"
            >
            </el-input>
          </div>
          <div class="c6 flex">
            <el-input
              :placeholder="$t('sys.t13')"
              :clearable="true"
              size="large"
              :prefix-icon="CollectionTag"
              v-model.trim="formData.code"
            >
            </el-input>
            <div class="c10">
              <img :src="yzmImg" alt="" @click="shuaxinYzm" v-if="yzmImg" />
            </div>
          </div>
          <el-button
            size="large"
            type="primary"
            class="c7"
            native-type="submit"
          >
            {{ $t("sys.t10") }}
          </el-button>
        </form>
      </div>
    </div>
    <div class="c20">
      <a :href="config.domain" target="_blank">{{ config.domain }}</a>
    </div>
  </div>
</template>

<script setup lang="ts">
import { post } from "@/common/http";
import { useAdminStore } from "@/store/user";
import config from "@/config";
import { ElMessage, ElLoading } from "element-plus";
import { User, Key, CollectionTag } from "@element-plus/icons-vue";

const router = useRouter();
const adminStore = useAdminStore();
const yzmImg = ref("");

const shuaxinYzm = () => {
  post("api/login/verify").then((res) => {
    yzmImg.value = config.baseUrl + res.data.src.slice(1) + "?" + Math.random();
  });
};
shuaxinYzm();

const formData = reactive({
  user: "admin",
  pass: "123456",
  code: "",
});
function subLogin(e:Event) {
  e.preventDefault();
  if (formData.user == "") return ElMessage.error("请输入账号");
  if (formData.pass == "") return ElMessage.error("请输入密码");
  if (formData.code == "") return ElMessage.error("请输入验证码");
  const loading = ElLoading.service({
    lock: true,
    text: "Loading",
    background: "rgba(0, 0, 0, 0.7)",
  });
  try {
    post("api/Admin/loginAdmin", formData).then((res) => {
      loading.close();
      if (res.code == 200) {
        localStorage.setItem("admin", JSON.stringify(res.data.admin));
        localStorage.setItem("token", res.data.token);
        adminStore.info = res.data.admin;
        adminStore.token = res.data.token;
        router.push("/");
      } else if (res.code == 201) {
        shuaxinYzm();
        ElMessage.error(res.msg);
      } else {
        ElMessage.error(res.msg);
      }
    });
  } catch (err) {
    loading.close();
    console.log(err);
  }
}
</script>
<style lang="scss" scoped>
.card {
  padding: 50px 30px;
  color: #fff;
  border-radius: 20px;
  overflow: hidden;
  background: linear-gradient(
    to right bottom,
    rgba(10, 10, 10, 0.4),
    rgba(10, 10, 10, 0.3),
    rgba(10, 10, 10, 0.2)
  );
  backdrop-filter: blur(8px);
  transform: scale(1);
  transition: backdrop-filter 0.3s, transform 0.3s;
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.38);
  transition: all 0.3s;
}
.card:hover {
  backdrop-filter: blur(10px);
  background: linear-gradient(
    to right bottom,
    rgba(10, 10, 10, 0.6),
    rgba(10, 10, 10, 0.4),
    rgba(10, 10, 10, 0.2)
  );
}

.root {
  position: relative;
  flex-direction: column;
  height: 100vh;
  justify-content: center;
  scroll-behavior: smooth;
  .bg {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: -1;
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
  }
  .c2 {
    width: 399px;
    text-align: center;
    margin-top: -10vh;
  }
  .c3 {
    margin-bottom: 10px;
    font-weight: 600;
    font-size: 32px;
    letter-spacing: 1px;
    color: #fff;
    text-align: center;
  }
  .c4 {
    font-weight: 300;
    color: #fff;
  }
  .c5 {
    margin-top: 30px;
  }
  .c6 {
    margin: 15px auto;
  }
  .c7 {
    width: 100%;
  }
  .c9 {
    position: fixed;
    left: 10px;
    top: 10px;
  }
  .c10 {
    width: 152px;
    margin-left: 10px;
  }
  .c10 img {
    width: 100%;
    height: 38px;
    position: relative;
    top: 1px;
    cursor: pointer;
  }
  .c20 {
    text-align: center;
    z-index: 9;
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
  }
  .c20 a {
    color: #fff;
    font-size: 16px;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.8);
  }
}
@media screen and (max-width: 640px) {
  .card {
    padding: 30px 20px;
  }
  .root {
    .c2 {
      width: 90%;
    }
  }
}
</style>
